package com.sprd.simple.launcher;

import java.util.List;

import com.sprd.simple.adapter.BrowseApplicationInfoAdapter;
import com.sprd.simple.model.AppInfo;
import com.sprd.simple.util.PackageInfoUtil;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;
import android.view.Window;

public class BrowserApplicationActivity extends BaseActivity<AppInfo> {
    private static final String TAG = "BrowsenActivity";

    private ListView mBrowserListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browser_appinfo);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                R.layout.browser_title_bar);

        mBrowserListView = (ListView) findViewById(R.id.browser_appinfo_folder_item);
        initListView(mBrowserListView, new BrowseApplicationInfoAdapter(
                this.getApplicationContext(), mInfos, appNameForTTS));
//        setSoftKey();
    }

    @Override
    protected List<AppInfo> loadData() {
        return getData();
    }

    private List<AppInfo> getData() {
        List<AppInfo> appInfos = PackageInfoUtil.getLauncherAttrAppForAppFolder(this);
        int size = appInfos.size();
        Log.i(TAG, "getData appInfos.size = " + size);
        appNameForTTS = new String[size];
        for (int i = 0; i < size; i++) {
            appNameForTTS[i] = appInfos.get(i).getAppLabel();
            Log.d(TAG, "app name = " + appNameForTTS[i]);
        }
        return appInfos;
    }

    @Override
    protected void launchApp(int position) {
        AppInfo appInfo = mInfos.get(position);
        if (appInfo == null) {
            return;
        }
        String packageName = appInfo.getAppPkg();
        try {
            Log.i(TAG, "launchApp package name = " + packageName);
            Intent intent = this.getPackageManager().getLaunchIntentForPackage(packageName);
            startActivity(intent);
        } catch (NullPointerException e) {
            Log.i(TAG, "Open error");
            if (PackageInfoUtil.PACKAGE_NAME_STK.equals(packageName)) {
                Toast.makeText(this, R.string.no_sim_card_inserted,
                        Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, R.string.app_open_error,
                        Toast.LENGTH_SHORT).show();
            }
        }
    }

}
